home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pas_0593.zip / DLTLINES.PAS < prev    next >
Pascal/Delphi Source File  |  1993-05-30  |  5KB  |  123 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 193 of 773
  3. From : Wayne Moses                         1:255/6.0            02 May 93  16:59
  4. To   : Ryan Matijcio
  5. Subj : Deleting Lines
  6. ────────────────────────────────────────────────────────────────────────────────
  7. *> Ryan Matijcio was heard mumbling to Wayne Moses on 01 May 93  11:26:48
  8. *> Regarding : Deleting Lines
  9.  
  10.  WM>  I see ... well you'll have to open the source file to read from, open a
  11.  WM>  temp file to write to, then on a line-by-line basis read(file1) and
  12.  WM>  write(file2) with the exception of the first five lines. Do this until
  13.  WM>  eof(file1), close both files, then copy the temp file onto file1.
  14.  
  15.  RM> Could you give me a sample of this code?  As I really don't
  16.  RM> know anything regarding file manipulation.
  17.  
  18.  Okay, Ryan ... here's some code for you. It ran well on my machine.
  19.  
  20.  -------- [ CUT LINE ]----------- 8< --------}
  21.  
  22.  Program DLTLINES;
  23.  
  24. {==========================================================================
  25.    Sample code for a program to delete "n" lines from a text file which
  26.    contains at least "n" lines. This code is in response to a query from
  27.    Ryan Matijcio.
  28.  
  29.    Written by    :  Wayne Moses
  30.    Written in    :  Turbo Pascal 6.0
  31.    Distribution  :  Public Domain
  32.    Date          :  May 2, 1993
  33.  
  34.    Usage syntax  :  dltlines textfilename [n]
  35.  
  36.                     where :  textfilename is the DOS full pathname
  37.                              n is the optional number of lines to delete;
  38.                                default is 5
  39.  
  40.    Note : Error trapping has, for the most part, been ignored.
  41. ==========================================================================}
  42. var
  43.    strTextFile, LineRead                     : string;
  44.    TextFile, Temp                            : text;
  45.    LinesToDelete, NumLines, code, i          : integer;
  46.  
  47. begin
  48.      (* First get the command line parameters *)
  49.  
  50.      if ParamCount = 0 then        { No parameters passes; display syntax }
  51.         begin                      { quit.                                }
  52.              writeln;
  53.              writeln('Usage syntax  :  dltlines textfilename [n]');
  54.              writeln;
  55.              writeln('where :  textfilename is the DOS full pathname');
  56.              writeln('         n is the optional number of lines to delete;');
  57.              writeln('         default is 5.');
  58.              halt;
  59.         end
  60.      else                          { Parameters were passed; no error    }
  61.          begin                     { checking is done in this code!      }
  62.               strTextFile := ParamStr(1);
  63.               if ParamCount = 1 then LinesToDelete := 5 else
  64.                  val(ParamStr(2), LinesToDelete, code);
  65.          end;
  66.  
  67.      (* Next, check if the file has more than LinesToDelete lines of text *)
  68.  
  69.      assign(TextFile,strTextFile);  { Associate the external file in first }
  70.      reset(TextFile);               { parameter with TextFile; prepare it  }
  71.      NumLines := 0;                 { for reading from ...                 }
  72.  
  73.      while not eof(TextFile) do
  74.            begin
  75.                 readln(TextFile,LineRead);
  76.                 inc(NumLines);
  77.            end;
  78.  
  79.      close(TextFile);
  80.  
  81.      if NumLines < LinesToDelete then   { In this case, print this note and }
  82.         begin                           { quit.                             }
  83.              writeln;
  84.              writeln('ERROR!! : ',strTextFile,' contains less than ',
  85.                      LinesToDelete,' lines.');
  86.              halt;
  87.         end;
  88.  
  89.      (* At this point, we can assume that the file has enough lines to
  90.         delete, so we begin deleting from the top. What we are really doing
  91.         is not writing all but the top LinesToDelete lines to a temp file *)
  92.  
  93.      assign(TextFile,strTextFile);
  94.      reset(TextFile);
  95.  
  96.      assign(Temp,'DLTLINES.TMP');
  97.      rewrite(Temp);
  98.  
  99.      (* First skip the first LinesToDelete lines *)
  100.  
  101.      for i := 1 to LinesToDelete do readln(TextFile, LineRead);
  102.  
  103.      (* Now read and write the balance of TextFile *)
  104.  
  105.      while not eof(TextFile) do
  106.            begin
  107.                 readln(TextFile, LineRead);
  108.                 writeln(Temp, LineRead);
  109.            end;
  110.  
  111.      close(TextFile); close(Temp);      { Close both files to save them }
  112.  
  113.      (* We now have the modified version of TextFile in DLTLINES.TMP on the
  114.         default drive. We want now to delete TextFile and rename
  115.         DLTLINES.TMP as strTextFile *)
  116.  
  117.      erase(TextFile); rename(Temp, strTextFile);
  118.  
  119.      (* There you go ... its done ... crude but done. It will do the trick.
  120.         You can add to this if you like, and compile it as an .EXE file
  121.         for further use. Have fun. *)
  122.  
  123. end.